package com.bss.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.andy.basics.Service;
import com.bss.model.Activity;
import com.bss.model.DailyDepositAndRefund;
import com.bss.service.DailyDepositAndRefundService;

@Controller
@Scope("request")
public class DailyDepositAndRefundController extends com.andy.basics.Controller<DailyDepositAndRefund> {

	@Autowired
	private DailyDepositAndRefundService dailyDepositAndRefundService;

	private boolean allowGenerate = true;

	private Date date = new Date();

	private DailyDepositAndRefund dailyDepositAndRefund;

	private DailyDepositAndRefund lastReport;

	public void setAllowGenerate(boolean allowGenerate) {
		this.allowGenerate = allowGenerate;
	}

	public boolean isAllowGenerate() {
		return this.allowGenerate;
	}

	@Override
	public Service<DailyDepositAndRefund> getService() {
		return this.dailyDepositAndRefundService;
	}

	@Override
	public DailyDepositAndRefund create() {
		return this.setUp(new Date());
	}

	@Override
	public String add() {
		String result = super.add();
		this.dailyDepositAndRefund = null;
		return result;
	}

	public DailyDepositAndRefund setUp(Date date) {

		DailyDepositAndRefund dailyDepositAndRefund = this.preSetup(date);

		this.setErrors(null);

		if (dailyDepositAndRefund.getRows() == 0) {
			this.setErrors(new ArrayList<String>());
			this.getErrors().add("Nothing found for this Date: " + dailyDepositAndRefund.getDate());
			this.allowGenerate = false;
		}

		return dailyDepositAndRefund;
	}

	protected DailyDepositAndRefund preSetup(Date date) {
		DailyDepositAndRefund dailyDepositAndRefund = new DailyDepositAndRefund();

		Activity activity = this.dailyDepositAndRefundService.getLastActivity();
		dailyDepositAndRefund.setToDate(date);
		dailyDepositAndRefund.setFromDate(activity == null ? null : activity.getActivityDate());
		dailyDepositAndRefund.setSecurityDeposit(this.dailyDepositAndRefundService.getSecurityDeposit());
		dailyDepositAndRefund.setUtensilDeposit(this.dailyDepositAndRefundService.getUtensilDeposit());
		dailyDepositAndRefund.setRoomDepositAndRefunds(this.dailyDepositAndRefundService.getRoomDepositAndRefunds(date));
		dailyDepositAndRefund.setUtensilDepositAndRefunds(this.dailyDepositAndRefundService.getUtensilDepositAndRefunds(date));

		this.dailyDepositAndRefundService.doTotal(dailyDepositAndRefund);
		dailyDepositAndRefund.setDate(date);
		return dailyDepositAndRefund;
	}

	@Override
	public String select() {

		Activity activity = this.dailyDepositAndRefundService.getLastActivity();
		if (activity != null) {
			Calendar cal1 = Calendar.getInstance();
			Calendar cal2 = Calendar.getInstance();
			cal1.setTime(this.getDate());
			cal2.setTime(activity.getActivityDate());
			if (!cal1.after(cal2)) {
				this.setDate(new Date());
				this.setModel(this.setUp(this.getDate()));
				if (this.getErrors() == null) {
					this.setErrors(new ArrayList<String>());
				} else {
					this.getErrors().clear();
				}
				this.getErrors()
						.add("INVALID DATE: Selected date must be greater than last report date !\nReport generation will be DISABLED. Please select a valid date");
				this.setError(true);

				this.allowGenerate = false;
				return null;
			} else {
				this.setErrors(new ArrayList<String>());
				this.setError(false);
			}
		}

		this.setModel(this.setUp(this.getDate()));
		return null;

	}

	public DailyDepositAndRefund getLastReport() {
		if (this.lastReport == null) {
			Activity activity = this.dailyDepositAndRefundService.getLastActivity();
			if (activity != null) {
				return this.lastReport = this.dailyDepositAndRefundService.create(activity.getActivityDate());
			}
		}
		return this.lastReport;
	}

	@Override
	public Date getDate() {
		return this.date;
	}

	@Override
	public void setDate(Date date) {
		this.date = date;
		super.setDate(this.getDate());
	}

	@Override
	public DailyDepositAndRefund getModel() {
		if (this.dailyDepositAndRefund == null) {
			return this.dailyDepositAndRefund = super.getModel();
		}
		return this.dailyDepositAndRefund;
	}
}